//
//  NewFeatureViewController.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/18.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit
import SnapKit
//新特性页面的数量
private let imageCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    init(){
        
        //        设置流水布局
        let layout = UICollectionViewFlowLayout()
        
        //        设置布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //        设置滑动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //        设置item的上下间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //
        super.init(collectionViewLayout: layout)
        //        分页
        collectionView?.pagingEnabled = true
        //        取消滚动条
        collectionView?.showsVerticalScrollIndicator = false
        //        取消弹簧效果
        collectionView?.bounces = false
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        //        每组有四个Item,一共4组
        return 4
    }
    
    //    cell的样式
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
    
        cell.index = indexPath.item
        
//        cell.showAnimationBtn()
        
        return cell
    }
    
//  当view加载完成后在执行弹出Btn的动画效果
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
//     显示 最后一个页面时，打印的是上一个页面的item
        print(indexPath.item)
//        获取准确的cell
    let cell = collectionView.visibleCells().last as! NewfeatureCell
        
//    获取item
        let index = collectionView.indexPathForCell(cell)!
        
        if index.item == imageCount - 1{
        
        cell.showAnimationBtn()
    
        }
        
    }
    
}

//
class NewfeatureCell : UICollectionViewCell{
    
    
//    没有给index赋初值，就会提示init方法没有初始化
    var index : Int = 0 {
        
    didSet {
        
        iconView.image = UIImage(named: "new_feature_\(index + 1)")
//        只在最后一个iew上显示btn
//        startBtn.hidden = index != 3
        
//       先让所有的页面都隐藏Btn
        startBtn.hidden = true
        
//        self.showAnimationBtn()
        
                }
            }
 
    
   override init(frame:CGRect){
    
        super.init(frame:frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    
    private func setupUI(){
                //        添加子控件
                contentView.addSubview(iconView)
                contentView.addSubview(startBtn)
        
                //        添加约束
                iconView.snp_makeConstraints { (make) -> Void in
        
                    make.edges.equalTo(contentView.snp_edges)
                }
                //        添加Btn的约束
                startBtn.snp_makeConstraints { (make) -> Void in
        
                    //        居中
                    make.centerX.equalTo(contentView.snp_centerX)
        
                    //       设置按钮的底部
                    make.bottom.equalTo(contentView.snp_bottom).offset(-180)
                }
//        添加点击事件
            startBtn.addTarget(self, action: "startBtnDidClicke", forControlEvents: .TouchUpInside)
            }
            //    创建一个iconView
            private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
            //    创建一个UIButton
            private lazy var startBtn : UIButton = {
        
                let btn = UIButton()
                btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState:.Normal)
                btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
                btn.setTitle("开始体验", forState:.Normal)
                
                btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
                //        自动适应
                btn.sizeToFit()
                return btn
            }()

//    当View页面加载完成后，做出动画效果加载出btn
    private func showAnimationBtn(){
    
//在动画开始之前，修改frame
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
//        在动画的闭包中修改按钮的形变
        /**
        <#Description#>
        
        - parameter delay:                  延迟
        - parameter usingSpringWithDamping: <#usingSpringWithDamping description#>
        - parameter initialSpringVelocity:  <#initialSpringVelocity description#>
        - parameter options:                <#options description#>
        - parameter animations:             <#animations description#>
        - parameter completion:             <#completion description#>
        */
        startBtn.hidden = false
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
          self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                print("OK")
        }
    }
    
//   添加 btn的点击事件
  @objc  private func startBtnDidClicke(){
    
    print(__FUNCTION__)
    
     NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    
    }
    
    
}

